import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'dart:async';

sealed class CounterEvent {}

final class CounterIncrementPressed extends CounterEvent {}

final class CounterDecrementPressed extends CounterEvent {}

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0) {
    on<CounterIncrementPressed>(
      (event, emit) async {
        addError(Exception('increment error!'), StackTrace.current);
        // handle incoming `CounterIncrementPressed` event
        emit(state + 1);
      },
      // transformer: debounce(const Duration(milliseconds: 300)),
    );
    on<CounterDecrementPressed>((event, emit) => emit(state - 1));
  }

  @override
  void onChange(Change<int> change) {
    super.onChange(change);
    print(change);
  }

  @override
  void onTransition(Transition<CounterEvent, int> transition) {
    super.onTransition(transition);
    print(transition);
  }

  @override
  void onEvent(CounterEvent event) {
    super.onEvent(event);
    print("event-----$event");
  }

  @override
  void onError(Object error, StackTrace stackTrace) {
    print('$error');
    super.onError(error, stackTrace);
  }
}
